/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.interaction;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JOptionPane;

import twaver.Element;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;
import demo.DemoPane;

public class InteractionDemo extends DemoPane {
	
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);

    public InteractionDemo() {
        this.add(network, BorderLayout.CENTER);

		ColorBackground background = new ColorBackground(Color.black);
		background.setGradientColor(Color.blue.darker().darker());
		background.setGradientFactory(TWaverConst.GRADIENT_EXTEND_E);
		background.setGradient(true);
		network.setBackground(background);
        
        new MouseHandler(network);
        
        int count = 18;
        int r = 280;
        for (int i = 0; i < count; i++) {
            Node node = new Node("node-" + i);
            node.setImage("/demo/resource/images/world.png");
            int x = (int) (r * Math.cos(Math.PI * 2 / count * i)) + 20;
            int y = (int) (r * Math.sin(Math.PI * 2 / count * i)) + 20;
            node.setLocation(r + x, r + y);
            node.putBorderColor(Color.GREEN);
            box.addElement(node);
        }
        for (int from = 0; from < count; from++) {
            for (int to = from + 1; to < count; to++) {
                Node fromNode = (Node) box.getElementByID("node-" + from);
                Node toNode = (Node) box.getElementByID("node-" + to);
                Link link = new Link("link-" + from + "-" + to, fromNode, toNode);
                link.putLinkOutlineWidth(0);
                link.putLinkWidth(1);
                link.putLinkColor(TWaverUtil.getRandomColor());
                link.putBorderColor(Color.WHITE);
                box.addElement(link);
            }
        }
        
        network.getCanvas().addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    Element element = network.getElementPhysicalAt(e.getPoint());
                    if (element == null) {
                        JOptionPane.showMessageDialog(network, "double click on nothing.");
                    } else {                   	
                    	JOptionPane.showMessageDialog(network, element.getID() + " is double clicked."); 
                    }	
                }
            }
        });

        // move mouse then select element.
		network.getCanvas().addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				Element element = network.getElementPhysicalAt(e.getPoint());
				if (element != null) {
					box.getSelectionModel().setSelection(element);
				}
			}
		});
    }

}